home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2003 November A / PCWK1103A.iso / ABBYY FineReader 7.0 PRO / data1.cab / gs_lev2.ps < prev    next >
Text File  |  2002-02-23  |  32KB  |  923 lines

  1. %    Copyright (C) 1990, 2000 Aladdin Enterprises.  All rights reserved.
  2. % This software is provided AS-IS with no warranty, either express or
  3. % implied.
  4. % This software is distributed under license and may not be copied,
  5. % modified or distributed except as expressly authorized under the terms
  6. % of the license contained in the file LICENSE in this distribution.
  7. % For more information about licensing, please refer to
  8. % http://www.ghostscript.com/licensing/. For information on
  9. % commercial licensing, go to http://www.artifex.com/licensing/ or
  10. % contact Artifex Software, Inc., 101 Lucas Valley Road #110,
  11. % San Rafael, CA  94903, U.S.A., +1(415)492-9861.
  12.  
  13. % $Id: gs_lev2.ps,v 1.7.2.3 2002/02/22 19:45:55 ray Exp $
  14. % Initialization file for Level 2 functions.
  15. % When this is run, systemdict is still writable,
  16. % but (almost) everything defined here goes into level2dict.
  17.  
  18. level2dict begin
  19.  
  20. % ------ System and user parameters ------ %
  21.  
  22. % User parameters must obey save/restore, and must also be maintained
  23. % per-context.  We implement the former, and some of the latter, here
  24. % with PostScript code.  NOTE: our implementation assumes that user
  25. % parameters change only as a result of setuserparams -- that there are
  26. % no user parameters that are ever changed dynamically by the interpreter
  27. % (although the interpreter may adjust the value presented to setuserparams)
  28. %
  29. % There are two types of user parameters: those which are actually
  30. % maintained in the interpreter, and those which exist only at the
  31. % PostScript level.  We maintain the current state of both types in
  32. % a read-only local dictionary named userparams, defined in systemdict.
  33. % In a multi-context system, each context has its own copy of this
  34. % dictionary.  In addition, there is a constant dictionary named
  35. % psuserparams where each key is the name of a user parameter that exists
  36. % only in PostScript and the value is a procedure to check that the value
  37. % is legal: setuserparams uses this for checking the values.
  38. % setuserparams updates userparams explicitly, in addition to setting
  39. % any user parameters in the interpreter; thus we can use userparams
  40. % to reset those parameters after a restore or a context switch.
  41. % NOTE: the name userparams is known to the interpreter, and in fact
  42. % the interpreter creates the userparams dictionary.
  43.  
  44. % Check parameters that are managed at the PostScript level.
  45. /.checkparamtype {        % <newvalue> <type> .checkparamtype <bool>
  46.   exch type eq
  47. } .bind def
  48. /.checksetparams {        % <newdict> <opname> <checkdict>
  49.                 %   .checksetparams <newdict>
  50.   2 index {
  51.         % Stack: newdict opname checkdict key newvalue
  52.     3 copy 3 1 roll .knownget {
  53.       exec not {
  54.     pop pop pop load /typecheck signalerror
  55.       } if
  56.       dup type /stringtype eq {
  57.     dup rcheck not {
  58.       pop pop pop load /invalidaccess signalerror
  59.     } if
  60.       } if
  61.     } {
  62.       pop
  63.     } ifelse pop pop
  64.   } forall pop pop
  65. } .bind def    % not odef, shouldn't reset stacks
  66.  
  67. % currentuser/systemparams creates and returns a dictionary in the
  68. % current VM.  The easiest way to make this work is to copy any composite
  69. % PostScript-level parameters to global VM.  Currently, the only such
  70. % parameters are strings.  In fact, we always copy string parameters,
  71. % so that we can be sure the contents won't be changed.
  72. /.copyparam {            % <value> .copyparam <value'>
  73.   dup type /stringtype eq {
  74.     .currentglobal true .setglobal
  75.     1 index length string exch .setglobal
  76.     copy readonly
  77.   } if
  78. } .bind def
  79.  
  80. % Some user parameters are managed entirely at the PostScript level.
  81. % We take care of that here.
  82. systemdict begin
  83. /psuserparams 48 dict def
  84. /getuserparam {            % <name> getuserparam <value>
  85.   /userparams .systemvar 1 index get exch pop
  86. } odef
  87. % Fill in userparams (created by the interpreter) with current values.
  88. mark .currentuserparams
  89. counttomark 2 idiv {
  90.   userparams 3 1 roll put
  91. } repeat pop
  92. /.definepsuserparam {        % <name> <value> .definepsuserparam -
  93.   psuserparams 3 copy pop
  94.   type cvlit /.checkparamtype cvx 2 packedarray cvx put
  95.   userparams 3 1 roll put
  96. } .bind def
  97. end
  98. /currentuserparams {        % - currentuserparams <dict>
  99.   /userparams .systemvar dup length dict .copydict
  100. } odef
  101. /setuserparams {        % <dict> setuserparams -
  102.     % Check that we will be able to set the PostScript-level
  103.     % user parameters.
  104.   /setuserparams /psuserparams .systemvar .checksetparams
  105.     % Set the C-level user params.  If this succeeds, we know that
  106.     % the password check succeeded.
  107.   dup .setuserparams
  108.     % Now set the PostScript-level params.
  109.     % The interpreter may have adjusted the values of some of the
  110.     % parameters, so we have to read them back.
  111.   dup {
  112.     /userparams .systemvar 2 index known {
  113.       psuserparams 2 index known not {
  114.     pop dup .getuserparam
  115.       } if
  116.       .copyparam
  117.       /userparams .systemvar 3 1 roll .forceput  % userparams is read-only
  118.     } {
  119.       pop pop
  120.     } ifelse
  121.   } forall
  122.     % A context switch might have occurred during the above loop,
  123.     % causing the interpreter-level parameters to be reset.
  124.     % Set them again to the new values.  From here on, we are safe,
  125.     % since a context switch will consult userparams.
  126.   .setuserparams
  127. } .bind odef
  128. % Initialize user parameters managed here.
  129. /JobName () .definepsuserparam
  130.  
  131. % Restore must restore the user parameters.
  132. % (Since userparams is in local VM, save takes care of saving them.)
  133. /restore {        % <save> restore -
  134.   //restore /userparams .systemvar .setuserparams
  135. } .bind odef
  136.  
  137. % The pssystemparams dictionary holds some system parameters that
  138. % are managed entirely at the PostScript level.
  139. systemdict begin
  140. currentdict /pssystemparams known not {
  141.   /pssystemparams 40 dict readonly def
  142. } if
  143. /getsystemparam {        % <name> getsystemparam <value>
  144.   //pssystemparams 1 index .knownget { exch pop } { .getsystemparam } ifelse
  145. } odef
  146. end
  147. /currentsystemparams {        % - currentsystemparams <dict>
  148.   mark .currentsystemparams //pssystemparams { } forall .dicttomark
  149. } odef
  150. /setsystemparams {        % <dict> setsystemparams -
  151.     % Check that we will be able to set the PostScript-level
  152.     % system parameters.
  153.    /SAFETY .systemvar /safe get {
  154.      % SAFER mode disallows some changes
  155.      [ /GenericResourceDir /FontResourceDir /GenericResourcePathSep ] {
  156.        2 copy .knownget {
  157.      exch //pssystemparams exch .knownget {
  158.            ne { /setsystemparams /invalidaccess signalerror } if
  159.          } {
  160.            pop
  161.          } ifelse
  162.        } {
  163.          pop
  164.        } ifelse
  165.      } forall
  166.    } if
  167.    /setsystemparams //pssystemparams mark exch {
  168.      type cvlit /.checkparamtype cvx 2 packedarray cvx
  169.    } forall .dicttomark .checksetparams
  170.     % Set the C-level system params.  If this succeeds, we know that
  171.     % the password check succeeded.
  172.    dup .setsystemparams
  173.     % Now set the PostScript-level params.  We must copy local strings
  174.     % into global VM.
  175.    dup
  176.     { //pssystemparams 2 index known
  177.        {        % Stack: key newvalue
  178.      .copyparam
  179.      //pssystemparams 3 1 roll .forceput    % pssystemparams is read-only
  180.        }
  181.        { pop pop
  182.        }
  183.       ifelse
  184.     }
  185.    forall pop
  186. } .bind odef
  187.  
  188. % Initialize the passwords.
  189. % NOTE: the names StartJobPassword and SystemParamsPassword are known to
  190. % the interpreter, and must be bound to noaccess strings.
  191. % The length of these strings must be max_password (iutil2.h) + 1.
  192. /StartJobPassword 65 string noaccess def
  193. /SystemParamsPassword 65 string noaccess def
  194.  
  195. % Redefine cache parameter setting to interact properly with userparams.
  196. /setcachelimit {
  197.   mark /MaxFontItem 2 index .dicttomark setuserparams pop
  198. } .bind odef
  199. /setcacheparams {
  200.     % The MaxFontCache parameter is a system parameter, which we might
  201.     % not be able to set.  Fortunately, this doesn't matter, because
  202.     % system parameters don't have to be synchronized between this code
  203.     % and the VM.
  204.   counttomark 1 add copy setcacheparams
  205.   currentcacheparams    % mark size lower upper
  206.     3 -1 roll pop
  207.     /MinFontCompress 3 1 roll
  208.     /MaxFontItem exch
  209.   .dicttomark setuserparams
  210.   cleartomark
  211. } .bind odef
  212.  
  213. % Add bogus user and system parameters to satisfy badly written PostScript
  214. % programs that incorrectly assume the existence of all the parameters
  215. % listed in Appendix C of the Red Book.  Note that some of these may become
  216. % real parameters later: code near the end of gs_init.ps takes care of
  217. % removing any such parameters from ps{user,system}params.
  218.  
  219. % psuserparams
  220.   /MaxFormItem 100000 .definepsuserparam
  221.   /MaxPatternItem 20000 .definepsuserparam
  222.   /MaxScreenItem 48000 .definepsuserparam
  223.   /MaxUPathItem 5000 .definepsuserparam
  224.  
  225. % File Access Permission parameters
  226.   .currentglobal true .setglobal
  227.   /.checkFilePermitparams {
  228.     type /arraytype eq {
  229.       currentuserparams /LockFilePermissions get {
  230.         5 { pop } repeat /setuserparams /invalidaccess signalerror
  231.       }
  232.       if
  233.     } {
  234.       5 { pop } repeat /setuserparams /typecheck signalerror
  235.     }
  236.     ifelse
  237.     true
  238.   } .bind def
  239. % Initialize the File Permission access control to wide open
  240. % These will only be accessed via current/set userparams.
  241. % Values are a string containing multiple nul terminated path strings
  242.   /PermitFileReading dup [ (*) ] .definepsuserparam
  243.     psuserparams exch /.checkFilePermitparams load put
  244.   /PermitFileWriting dup [ (*) ] .definepsuserparam
  245.     psuserparams exch /.checkFilePermitparams load put
  246.   /PermitFileControl dup [ (*) ] .definepsuserparam
  247.     psuserparams exch /.checkFilePermitparams load put
  248.   .setglobal
  249.  
  250. pssystemparams begin
  251.   /CurDisplayList 0 .forcedef
  252.   /CurFormCache 0 .forcedef
  253.   /CurOutlineCache 0 .forcedef
  254.   /CurPatternCache 0 .forcedef
  255.   /CurUPathCache 0 .forcedef
  256.   /CurScreenStorage 0 .forcedef
  257.   /CurSourceList 0 .forcedef
  258.   /DoPrintErrors false .forcedef
  259.   /MaxDisplayList 140000 .forcedef
  260.   /MaxFormCache 100000 .forcedef
  261.   /MaxOutlineCache 65000 .forcedef
  262.   /MaxPatternCache 100000 .forcedef
  263.   /MaxUPathCache 300000 .forcedef
  264.   /MaxScreenStorage 84000 .forcedef
  265.   /MaxSourceList 25000 .forcedef
  266.   /RamSize 4194304 .forcedef
  267. end
  268.  
  269. % Define the procedures for handling comment scanning.  The names
  270. % %ProcessComment and %ProcessDSCComment are known to the interpreter.
  271. % These procedures take the file and comment string and file as operands.
  272. /.checkprocesscomment {
  273.   dup null eq {
  274.     pop true
  275.   } {
  276.     dup xcheck {
  277.       type dup /arraytype eq exch /packedarraytype eq or
  278.     } {
  279.       pop false
  280.     } ifelse
  281.   } ifelse
  282. } .bind def
  283. /ProcessComment null .definepsuserparam
  284. psuserparams /ProcessComment {.checkprocesscomment} put
  285. (%ProcessComment) cvn {
  286.   /ProcessComment getuserparam
  287.   dup null eq { pop pop pop } { exec } ifelse
  288. } bind def
  289. /ProcessDSCComment null .definepsuserparam
  290. psuserparams /ProcessDSCComment {.checkprocesscomment} put
  291. (%ProcessDSCComment) cvn {
  292.   /ProcessDSCComment getuserparam
  293.   dup null eq { pop pop pop } { exec } ifelse
  294. } bind def
  295.  
  296. % ------ Miscellaneous ------ %
  297.  
  298. (<<) cvn            % - << -mark-
  299.   /mark load def
  300. (>>) cvn            % -mark- <key1> <value1> ... >> <dict>
  301.   /.dicttomark load def
  302. /languagelevel 2 def
  303. % When running in Level 2 mode, this interpreter is supposed to be
  304. % compatible with Adobe version 2017.
  305. /version (2017) readonly def
  306.  
  307. % If binary tokens are supported by this interpreter,
  308. % set an appropriate default binary object format.
  309. /setobjectformat where
  310.  { pop
  311.    /RealFormat getsystemparam (IEEE) eq { 1 } { 3 } ifelse
  312.    /ByteOrder getsystemparam { 1 add } if
  313.    setobjectformat
  314.  } if
  315.  
  316. % Aldus Freehand versions 2.x check for the presence of the
  317. % setcolor operator, and if it is missing, substitute a procedure.
  318. % Unfortunately, the procedure takes different parameters from
  319. % the operator.  As a result, files produced by this application
  320. % cause an error if the setcolor operator is actually defined
  321. % and 'bind' is ever used.  Aldus fixed this bug in Freehand 3.0,
  322. % but there are a lot of files created by the older versions
  323. % still floating around.  Therefore, at Adobe's suggestion,
  324. % we implement the following dreadful hack in the 'where' operator:
  325. %      If the key is /setcolor, and
  326. %        there is a dictionary named FreeHandDict, and
  327. %        currentdict is that dictionary,
  328. %      then "where" consults only that dictionary and not any other
  329. %        dictionaries on the dictionary stack.
  330. .wheredict /setcolor {
  331.   /FreeHandDict .where {
  332.     /FreeHandDict get currentdict eq {
  333.       pop currentdict /setcolor known { currentdict true } { false } ifelse
  334.     } {
  335.       .where
  336.     } ifelse
  337.   } {
  338.     .where
  339.   } ifelse
  340. } bind put
  341.  
  342. % ------ Virtual memory ------ %
  343.  
  344. /currentglobal            % - currentglobal <bool>
  345.   /currentshared load def
  346. /gcheck                % <obj> gcheck <bool>
  347.   /scheck load def
  348. /setglobal            % <bool> setglobal -
  349.   /setshared load def
  350. % We can make the global dictionaries very small, because they auto-expand.
  351. /globaldict currentdict /shareddict .knownget not { 4 dict } if def
  352. /GlobalFontDirectory SharedFontDirectory def
  353.  
  354. % VMReclaim and VMThreshold are user parameters.
  355. /setvmthreshold {        % <int> setvmthreshold -
  356.   mark /VMThreshold 2 index .dicttomark setuserparams pop
  357. } odef
  358. /vmreclaim {            % <int> vmreclaim -
  359.   dup 0 gt {
  360.     .vmreclaim
  361.   } {
  362.     mark /VMReclaim 2 index .dicttomark setuserparams pop
  363.   } ifelse
  364. } odef
  365. -1 setvmthreshold
  366.  
  367. % ------ IODevices ------ %
  368.  
  369. /.getdevparams where {
  370.   pop /currentdevparams {    % <iodevice> currentdevparams <dict>
  371.     .getdevparams .dicttomark
  372.   } odef
  373. } if
  374. /.putdevparams where {
  375.   pop /setdevparams {        % <iodevice> <dict> setdevparams -
  376.     mark 1 index { } forall counttomark 2 add index
  377.     .putdevparams pop pop
  378.   } odef
  379. } if
  380.  
  381. % ------ Job control ------ %
  382.  
  383. serverdict begin
  384.  
  385. % We could protect the job information better, but we aren't attempting
  386. % (currently) to protect ourselves against maliciousness.
  387.  
  388. /.jobsave null def        % top-level save object
  389. /.jobsavelevel 0 def        % save depth of job (0 if .jobsave is null,
  390.                 % 1 otherwise)
  391. /.adminjob true def        % status of current unencapsulated job
  392.  
  393. end        % serverdict
  394.  
  395. % Because there may be objects on the e-stack created since the job save,
  396. % we have to clear the e-stack before doing the end-of-job restore.
  397. % We do this by executing a 2 .stop, which is caught by the 2 .stopped
  398. % in .runexec; we leave on the o-stack a procedure to execute aftewards.
  399. %
  400. %**************** The definition of startjob is not complete yet, since
  401. % it doesn't reset stdin/stdout.
  402. /.startnewjob {            % <exit_bool> <password_level>
  403.                 %   .startnewjob -
  404.     serverdict /.jobsave get dup null eq { pop } { restore } ifelse
  405.     exch {
  406.             % Unencapsulated job
  407.       serverdict /.jobsave null put
  408.       serverdict /.jobsavelevel 0 put
  409.       serverdict /.adminjob 3 -1 roll 1 gt put
  410.         % The Adobe documentation doesn't say what happens to the
  411.         % graphics state stack in this case, but an experiment
  412.         % produced results suggesting that a grestoreall occurs.
  413.       grestoreall
  414.     } {
  415.             % Encapsulated job
  416.       pop
  417.       serverdict /.jobsave save put
  418.       serverdict /.jobsavelevel 1 put
  419.     } ifelse
  420.         % Reset the interpreter state.
  421.   clear cleardictstack
  422.   initgraphics
  423.   false setglobal
  424. } bind def
  425. /.startjob {            % <exit_bool> <password> <finish_proc>
  426.                 %   .startjob <ok_bool>
  427.   vmstatus pop pop serverdict /.jobsavelevel get eq
  428.   2 index .checkpassword 0 gt and {
  429.     exch .checkpassword exch count 3 roll count 3 sub { pop } repeat
  430.     cleardictstack
  431.         % Reset the e-stack back to the 2 .stopped in .runexec,
  432.         % passing the finish_proc to be executed afterwards.
  433.     2 .stop
  434.   } {        % Password check failed
  435.     pop pop pop false
  436.   } ifelse
  437. } odef
  438. /startjob {            % <exit_bool> <password> startjob <ok_bool>
  439.     % This is a hack.  We really need some way to indicate explicitly
  440.     % to the interpreter that we are under control of a job server.
  441.   .userdict /quit /stop load put
  442.   { .startnewjob true } .startjob
  443. } odef
  444.  
  445. systemdict begin
  446. /quit {                % - quit -
  447.   //systemdict begin serverdict /.jobsave get null eq
  448.    { end //quit }
  449.    { /quit load /invalidaccess /signalerror load end exec }
  450.   ifelse
  451. } bind odef
  452. end
  453.  
  454. % We would like to define exitserver as a procedure, using the code
  455. % that the Red Book says is equivalent to it.  However, since startjob
  456. % resets the exec stack, we can't do this, because control would never
  457. % proceed past the call on startjob if the exitserver is successful.
  458. % Instead, we need to construct exitserver out of pieces of startjob.
  459.  
  460. serverdict begin
  461.  
  462. /exitserver {            % <password> exitserver -
  463.   true exch { .startnewjob } .startjob not {
  464.     /exitserver /invalidaccess signalerror
  465.   } if
  466. } bind def
  467.  
  468. end        % serverdict
  469.  
  470. % ------ Compatibility ------ %
  471.  
  472. % In Level 2 mode, the following replace the definitions that gs_statd.ps
  473. % installs in statusdict and serverdict.
  474. % Note that statusdict must be allocated in local VM.
  475. % We don't bother with many of these yet.
  476.  
  477. /.dict1 { exch mark 3 1 roll .dicttomark } bind def
  478.  
  479. currentglobal false setglobal 25 dict exch setglobal begin
  480. currentsystemparams
  481.  
  482. % The following do not depend on the presence of setpagedevice.
  483. /buildtime 1 index /BuildTime get def
  484. /byteorder 1 index /ByteOrder get def
  485. /checkpassword { .checkpassword 0 gt } bind def
  486. dup /DoStartPage known
  487.  { /dostartpage { /DoStartPage getsystemparam } bind def
  488.    /setdostartpage { /DoStartPage .dict1 setsystemparams } bind def
  489.  } if
  490. dup /StartupMode known
  491.  { /dosysstart { /StartupMode getsystemparam 0 ne } bind def
  492.    /setdosysstart { { 1 } { 0 } ifelse /StartupMode .dict1 setsystemparams } bind def
  493.  } if
  494. %****** Setting jobname is supposed to set userparams.JobName, too.
  495. /jobname { /JobName getuserparam } bind def
  496. /jobtimeout { /JobTimeout getuserparam } bind def
  497. /ramsize { /RamSize getsystemparam } bind def
  498. /realformat 1 index /RealFormat get def
  499. dup /PrinterName known
  500.  { /setprintername { /PrinterName .dict1 setsystemparams } bind def
  501.  } if
  502. /printername
  503.  { currentsystemparams /PrinterName .knownget not { () } if exch copy
  504.  } bind def
  505. currentuserparams /WaitTimeout known
  506.  { /waittimeout { /WaitTimeout getuserparam } bind def
  507.  } if
  508.  
  509. % The following do require setpagedevice.
  510. /.setpagedevice where { pop } { (%END PAGEDEVICE) .skipeof } ifelse
  511. /defaulttimeouts
  512.  { currentsystemparams dup
  513.    /JobTimeout .knownget not { 0 } if
  514.    exch /WaitTimeout .knownget not { 0 } if
  515.    currentpagedevice /ManualFeedTimeout .knownget not { 0 } if
  516.  } bind def
  517. /margins
  518.  { currentpagedevice /Margins .knownget { exch } { [0 0] } ifelse
  519.  } bind def
  520. /pagemargin
  521.  { currentpagedevice /PageOffset .knownget { 0 get } { 0 } ifelse
  522.  } bind def
  523. /pageparams
  524.  { currentpagedevice
  525.    dup /Orientation .knownget { 1 and ORIENT1 { 1 xor } if } { 0 } ifelse exch
  526.    dup /PageSize get aload pop 3 index 0 ne { exch } if 3 2 roll
  527.    /PageOffset .knownget { 0 get } { 0 } ifelse 4 -1 roll
  528.  } bind def
  529. /setdefaulttimeouts
  530.  { exch mark /ManualFeedTimeout 3 -1 roll
  531.    /Policies mark /ManualFeedTimeout 1 .dicttomark
  532.    .dicttomark setpagedevice
  533.    /WaitTimeout exch mark /JobTimeout 5 2 roll .dicttomark setsystemparams
  534.  } bind def
  535. /.setpagesize { 2 array astore /PageSize .dict1 setpagedevice } bind def
  536. /setduplexmode { /Duplex .dict1 setpagedevice } bind def
  537. /setmargins
  538.  { exch 2 array astore /Margins .dict1 setpagedevice
  539.  } bind def
  540. /setpagemargin { 0 2 array astore /PageOffset .dict1 setpagedevice } bind def
  541. /setpageparams
  542.  { mark /PageSize 6 -2 roll
  543.    4 index 1 and ORIENT1 { 1 } { 0 } ifelse ne { exch } if 2 array astore
  544.    /Orientation 5 -1 roll ORIENT1 { 1 xor } if
  545.    /PageOffset counttomark 2 add -1 roll 0 2 array astore
  546.    .dicttomark setpagedevice
  547.  } bind def
  548. /setresolution
  549.  { dup 2 array astore /HWResolution .dict1 setpagedevice
  550.  } bind def
  551. %END PAGEDEVICE
  552.  
  553. % The following are not implemented yet.
  554. %manualfeed
  555. %manualfeedtimeout
  556. %pagecount
  557. %pagestackorder
  558. %setpagestackorder
  559.  
  560. pop        % currentsystemparams
  561.  
  562. % Flag the current dictionary so it will be swapped when we
  563. % change language levels.  (See zmisc2.c for more information.)
  564. /statusdict currentdict def
  565.  
  566. currentdict end
  567. /statusdict exch .forcedef    % statusdict is local, systemdict is global
  568.  
  569. % The following compatibility operators are in systemdict.  They are
  570. % defined here, rather than in gs_init.ps, because they require the
  571. % resource machinery.
  572.  
  573. /devforall {        % <pattern> <proc> <scratch> devforall -
  574.   exch {
  575.     1 index currentdevparams
  576.     /Type .knownget { /FileSystem eq } { false } ifelse
  577.     { exec } { pop pop } ifelse
  578.   } /exec load 3 packedarray cvx exch
  579.   (*) 3 1 roll ppstack flush /IODevice resourceforall
  580. } odef
  581. /devstatus {        % <(%disk*%)> devstatus <searchable> <writable>
  582.             %   <hasNames> <mounted> <removable> <searchOrder>
  583.             %   <freePages> <size> true
  584.             % <string> devstatus false
  585.   dup length 5 ge {
  586.     dup 0 5 getinterval (%disk) eq {
  587.       dup /IODevice resourcestatus {
  588.     pop pop dup currentdevparams
  589.     dup /Searchable get
  590.     exch dup /Writable get
  591.     exch dup /HasNames get
  592.     exch dup /Mounted get
  593.     exch dup /Removable get
  594.     exch dup /SearchOrder get
  595.     exch dup /Free get
  596.     exch /LogicalSize get
  597.     9 -1 roll pop true
  598.       } {
  599.     pop false
  600.       } ifelse
  601.     } {
  602.       pop false
  603.     } ifelse
  604.   } {
  605.     pop false
  606.   } ifelse
  607. } odef
  608.  
  609. % ------ Color spaces ------ %
  610.  
  611. % Attempt to convert a tint transformation procedure to a type 4 Function.
  612. % The value <m> is the number  of function inputs
  613. % The current color space defines the number of function output values.
  614. % The current color space will be the alternate color space for the function.
  615. % If the conversion fails then build a color cube function.
  616.  
  617. /.converttinttransform {    % [.. .. .. proc ] <m>
  618.                 %   .converttinttransform [.. .. .. proc']
  619.   .currentglobal            % Save current global memory state
  620.   2 index gcheck .setglobal        % Set gobal mode to match the array's mode
  621.   4 dict                % Build a dictionary for our type 4 function
  622.     dup /FunctionType 4 put        % Set FunctionType
  623.     dup /Function 5 index 3 get put    % Set function expression
  624.         % Stack: orig m global func
  625.     dup /Domain
  626.     [ 5 index {0 1} repeat ] put    % set Domain values
  627.     dup /Range
  628.     [ mark currentcolor counttomark
  629.     dup 2 add 1 roll cleartomark    % # of components in alternate space
  630.     {0 1} repeat ] put            % Set Range values
  631.   { .buildfunction } .internalstopped    % Try to build a type 4 function
  632.   dup
  633.   {                % type 4 function failed - Collect data for a color cube
  634.     pop                    % Remove duplicate copy of stopped status
  635.     pop                    % Remove invalid type 4 function
  636.     1 index                % Get number of inputs
  637.     mark currentcolor counttomark    % Count number of output colors
  638.     dup 2 add 1 roll cleartomark    % # of components in alternate space
  639.     4 index 3 get            % Get tint transform function
  640.     { .buildcolorcube } .internalstopped 
  641.   } if
  642.   {                    % Color cube build failed
  643.     pop pop pop exch pop exch pop    % Remove unused parameters
  644.     .setglobal                % Restore global state
  645.   } {                    % Function build succeeded - install function
  646.                     % Stack: orig m global func
  647.     3 -1 roll pop            % Stack: orig global func
  648.     2 index 4 array copy dup 3 4 -1 roll put
  649.     exch .setglobal exch pop
  650.   } ifelse
  651. } bind def
  652.  
  653. % Define the setcolorspace procedures:
  654. %    <colorspace> proc <colorspace'|null>
  655. % We have to define the dictionary first, so it can be bound into the
  656. % implementation procedure, but we can't populate it until the procedure
  657. % has been defined, so that the procedure can get bound into recursive calls.
  658. /colorspacedict 20 dict def
  659.  
  660. /.devcs [
  661.   /DeviceGray /DeviceRGB /DeviceCMYK /DevicePixel
  662. ] readonly def
  663. /currentcolorspace {        % - currentcolorspace <array>
  664.   .currentcolorspace dup type /integertype eq {
  665.     //.devcs exch 1 getinterval
  666.   } if
  667. } odef
  668. currentdict /.devcs .undef
  669.  
  670. /setcolorspace {        % <name|array> setcolorspace -
  671.   dup dup dup type /nametype ne { 0 get } if
  672.   //colorspacedict exch get exec
  673.   dup null eq { pop } { .setcolorspace } ifelse pop
  674. } odef
  675.  
  676. colorspacedict
  677.   dup /DeviceGray { pop 0 setgray null } bind put
  678.   dup /DeviceRGB { pop 0 0 0 setrgbcolor null } bind put
  679.   /setcmykcolor where
  680.    { pop dup /DeviceCMYK { pop 0 0 0 1 setcmykcolor null } bind put
  681.    } if
  682.   /.setcieaspace where
  683.    { pop dup /CIEBasedA { NOCIE { pop 0 setgray null } { dup 1 get .setcieaspace } ifelse } bind put
  684.    } if
  685.   /.setcieabcspace where
  686.    { pop dup /CIEBasedABC { NOCIE { pop 0 0 0 setrgbcolor null } { dup 1 get .setcieabcspace } ifelse } bind put
  687.    } if
  688.   /.setciedefspace where
  689.    { pop dup /CIEBasedDEF { NOCIE { pop 0 0 0 setrgbcolor null } { dup 1 get .setciedefspace } ifelse } bind put
  690.    } if
  691.   /.setciedefgspace where
  692.    { pop dup /CIEBasedDEFG { NOCIE { pop 0 0 0 1 setcmykcolor null } { dup 1 get .setciedefgspace } ifelse } bind put
  693.    } if
  694.   /.setseparationspace where
  695.    { pop dup /Separation { dup 2 get setcolorspace dup 1 .converttinttransform .setseparationspace } bind put
  696.    } if
  697.   /.setindexedspace where
  698.    { pop dup /Indexed { dup 1 get setcolorspace dup .setindexedspace } bind put
  699.    } if
  700.   /.nullpatternspace [/Pattern] readonly def
  701.   /.setpatternspace where
  702.    { pop dup /Pattern
  703.       { dup type /nametype eq { pop //.nullpatternspace } if
  704.     dup length 1 gt { dup 1 get setcolorspace } if
  705.         dup .setpatternspace
  706.       } bind put
  707.    } if
  708.     % If DeviceN space is included, gs_ll3.ps registers it.
  709.   /.setdevicepixelspace where
  710.    { pop dup /DevicePixel { dup .setdevicepixelspace } bind put
  711.    } if
  712.   currentdict /.nullpatternspace .undef
  713. pop
  714.  
  715. % ------ CIE color rendering ------ %
  716.  
  717. % Define findcolorrendering and a default ColorRendering ProcSet.
  718.  
  719. /findcolorrendering {        % <intentname> findcolorrendering
  720.                 %   <crdname> <found>
  721.   /ColorRendering /ProcSet findresource
  722.   1 index .namestring (.) concatstrings
  723.   1 index /GetPageDeviceName get exec .namestring (.) concatstrings
  724.   2 index /GetHalftoneName get exec .namestring
  725.   concatstrings concatstrings
  726.   dup /ColorRendering resourcestatus {
  727.     pop pop exch pop exch pop true
  728.   } {
  729.     pop /GetSubstituteCRD get exec false
  730.   } ifelse
  731. } odef
  732.  
  733. 5 dict dup begin
  734.  
  735. /GetPageDeviceName {        % - GetPageDeviceName <name>
  736.   currentpagedevice dup /PageDeviceName .knownget {
  737.     exch pop dup null eq { pop /none } if
  738.   } {
  739.     pop /none
  740.   } ifelse
  741. } bind def
  742.  
  743. /GetHalftoneName {        % - GetHalftoneName <name>
  744.   currenthalftone /HalftoneName .knownget not { /none } if
  745. } bind def
  746.  
  747. /GetSubstituteCRD {        % <intentname> GetSubstituteCRD <crdname>
  748.   pop /DefaultColorRendering
  749. } bind def
  750.  
  751. end
  752. % The resource machinery hasn't been activated, so just save the ProcSet
  753. % and let .fixresources finish the installation process.
  754. /ColorRendering exch def
  755.  
  756. % Define setcolorrendering.
  757.  
  758. /.colorrenderingtypes 5 dict def
  759.  
  760. /setcolorrendering {        % <crd> setcolorrendering -
  761.   dup /ColorRenderingType get //.colorrenderingtypes exch get exec
  762. } odef
  763.  
  764. /.setcolorrendering1 where { pop } { (%END CRD) .skipeof } ifelse
  765.  
  766. .colorrenderingtypes 1 {
  767.   dup .buildcolorrendering1 .setcolorrendering1
  768. } .bind put
  769.  
  770. % Note: the value 101 in the next line must be the same as the value of
  771. % GX_DEVICE_CRD1_TYPE in gscrdp.h.
  772. .colorrenderingtypes 101 {
  773.   dup .builddevicecolorrendering1 .setdevicecolorrendering1
  774. } .bind put
  775.  
  776. % Initialize the default CIE rendering dictionary.
  777. % The most common CIE files seem to assume the "calibrated RGB color space"
  778. % described on p. 189 of the PostScript Language Reference Manual,
  779. % 2nd Edition; we simply invert this transformation back to RGB.
  780. mark
  781.    /ColorRenderingType 1
  782. % We must make RangePQR and RangeLMN large enough so that values computed by
  783. % the assumed encoding MatrixLMN don't get clamped.
  784.    /RangePQR [0 0.9505 0 1 0 1.0890] readonly
  785. % This TransformPQR implements a relative colorimetric intent by scaling
  786. % the XYZ values relative to the white and black points.
  787.    /TransformPQR
  788.      [ { 5 1 roll                    % p Ws Bs Wd Bd
  789.          4 {3 get 5 1 roll} repeat   % ws bs wd bd p 
  790.          3 index sub                 % ws bs wd bd p-bs 
  791.          1 index                     % ws bs wd bd p-bs bd
  792.          6 2 roll                    % p-bs bd ws bs wd bd 
  793.          sub                         % p-bs bd ws bs wd-bd 
  794.          5 1 roll                    % wd-bd p-bs bd ws bs  
  795.          sub                         % wd-bd p-bs bd ws-bs  
  796.          4 2 roll                    % bd ws-bs wd-bd p-bs 
  797.          mul                         % bd ws-bs (wd-bd)*(p-bs)
  798.          exch div add                % bd + (wd-bd)*(p-bs)/(ws-bs)
  799.        } bind
  800.        { 5 1 roll
  801.          4 {4 get 5 1 roll} repeat
  802.          3 index sub 1 index 6 2 roll sub 5 1 roll
  803.          sub 4 2 roll mul exch div add
  804.        } bind
  805.        { 5 1 roll
  806.          4 {5 get 5 1 roll} repeat
  807.          3 index sub 1 index 6 2 roll sub 5 1 roll
  808.          sub 4 2 roll mul exch div add
  809.        } bind
  810.      ] readonly
  811.    /RangeLMN [0 0.9505 0 1 0 1.0890] readonly
  812.    /MatrixABC
  813.     [ 3.24063 -0.96893  0.05571
  814.      -1.53721  1.87576 -0.20402
  815.      -0.49863  0.04152  1.05700
  816.     ] readonly
  817.    /EncodeABC [ {0 .max 0.45 exp} bind dup dup] readonly
  818.    /WhitePoint [0.9505 1 1.0890] readonly
  819.     % Some Genoa tests seem to require the presence of BlackPoint.
  820.    /BlackPoint [0 0 0] readonly
  821. .dicttomark setcolorrendering
  822.  
  823. %END CRD
  824.  
  825. % Initialize a CIEBased color space for sRGB.
  826. /CIEsRGB [ /CIEBasedABC
  827.   mark
  828.     /DecodeLMN [ {
  829.       dup 0.03928 le { 12.92321 div } { 0.055 add 1.055 div 2.4 exp } ifelse
  830.     } bind dup dup ] readonly
  831.     /MatrixLMN [
  832.       0.412457 0.212673 0.019334
  833.       0.357576 0.715152 0.119192
  834.       0.180437 0.072175 0.950301
  835.     ] readonly
  836.     /WhitePoint [0.9505 1.0 1.0890] readonly
  837.   .dicttomark readonly
  838. ] readonly def
  839.  
  840. % ------ Painting ------ %
  841.  
  842. % A straightforward definition of execform that doesn't actually
  843. % do any caching.
  844. /.execform1 {
  845.     % This is a separate operator so that the stacks will be restored
  846.     % properly if an error occurs.
  847.   dup /Matrix get concat
  848.   dup /BBox get aload pop
  849.   exch 3 index sub exch 2 index sub rectclip
  850.   dup /PaintProc get
  851.   1 index /Implementation known not {
  852.     1 index dup /Implementation null .forceput readonly pop
  853.   } if
  854.   exec
  855. } .bind odef    % must bind .forceput
  856.  
  857. /.formtypes 5 dict
  858.   dup 1 /.execform1 load put
  859. def
  860.  
  861. /execform {            % <form> execform -
  862.   gsave {
  863.     dup /FormType get //.formtypes exch get exec
  864.   } stopped grestore { stop } if
  865. } odef
  866.  
  867. /.patterntypes 5 dict
  868.   dup 1 /.buildpattern1 load put
  869. def
  870.  
  871. /makepattern {            % <proto_dict> <matrix> makepattern <pattern>
  872.   //.patterntypes 2 index /PatternType get get
  873.   .currentglobal false .setglobal exch
  874.         % Stack: proto matrix global buildproc
  875.   3 index dup length 1 add dict .copydict
  876.   3 index 3 -1 roll exec 3 -1 roll .setglobal
  877.   1 index /Implementation 3 -1 roll put
  878.   readonly exch pop exch pop
  879. } odef
  880.  
  881. /setpattern {            % [<comp1> ...] <pattern> setpattern -
  882.   currentcolorspace 0 get /Pattern ne {
  883.     [ /Pattern currentcolorspace ] setcolorspace
  884.   } if setcolor
  885. } odef
  886.  
  887. % Extend image and imagemask to accept dictionaries.
  888. % We must create .imagetypes and .imagemasktypes outside level2dict,
  889. % and leave some extra space because we're still in Level 1 mode.
  890. systemdict begin
  891. /.imagetypes 5 dict
  892.   dup 1 /.image1 load put
  893. def
  894. /.imagemasktypes 5 dict
  895.   dup 1 /.imagemask1 load put
  896. def
  897. end
  898.  
  899. /.image /image load def
  900. /image {
  901.   dup type /dicttype eq {
  902.     dup /ImageType get //.imagetypes exch get exec
  903.   } {
  904.     //.image
  905.   } ifelse
  906. } odef
  907. currentdict /.image undef
  908.  
  909. /.imagemask /imagemask load def
  910. /imagemask {
  911.   dup type /dicttype eq {
  912.     dup /ImageType get //.imagemasktypes exch get exec
  913.   } {
  914.     //.imagemask
  915.   } ifelse
  916. } odef
  917. currentdict /.imagemask undef
  918.  
  919. end                % level2dict
  920.